package com.example.pkmedical.Fragments;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.RemindNextActivity;
import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SerialProtUtil;
import com.example.pkmedical.Utils.TestTime;

import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

import static com.example.pkmedical.Utils.TimeUtils.generateTime;

public class RemindFragment extends BaseFragment {
    @BindView(R.id.tixing_item_close)
    RelativeLayout tixingItemClose;
    @BindView(R.id.tixing_item_zhendong_item)
    RelativeLayout tixingItemZhendongItem;
    @BindView(R.id.tixing_item_fanshen_item)
    RelativeLayout tixing_item_fanshen_item;
    @BindView(R.id.tixing_item_fanshen_item_text)
    TextView tixing_item_fanshen_item_text;
    @BindView(R.id.tixing_item_zhendong_item_text)
    TextView tixingItemZhendongItemText;
    @BindView(R.id.tixing_item_close_text)
    TextView tixingItemCloseText;
    @BindView(R.id.cf_time)
    TextView cfTime;
    @BindView(R.id.qjzd_time)
    TextView qjzdTime;
    @BindView(R.id.fs_time)
    TextView fsTime;


    private PopupWindow qjzdPop, fsPop, cefanPop;
    private int cefanType = 0;
    private int qiaojiType = 10;
    private int fanshenType = 20;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_remind;
    }

    @Override
    public void findViewById(View view) {

    }

    @Override
    public void setViewData(View view) {

    }

    @Override
    public void setClickEvent(View view) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            //TODO 不可见操作
            if (qjzdPop != null){
                qjzdPop.dismiss();
            }
            if (cefanPop != null){
                cefanPop.dismiss();
            }
            if (fsPop != null){
                fsPop.dismiss();
            }
        } else {
            //TODO 可见操作

        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.USER_MESSAGE_TIME) {
            cfTime.setVisibility(View.GONE);
            tixingItemCloseText.setText(R.string.remind_off);
            qjzdTime.setVisibility(View.GONE);
            tixingItemZhendongItemText.setText(R.string.remind_off);
            fsTime.setVisibility(View.GONE);
            tixing_item_fanshen_item_text.setText(R.string.remind_off);
            if (mTimeCountDisposableFS != null) {
                //取消上一次的订阅
                mTimeCountDisposableFS.dispose();
            }
            if (mTimeCountDisposableCF != null) {
                //取消上一次的订阅
                mTimeCountDisposableCF.dispose();
            }
            if (mTimeCountDisposableQJZD != null) {
                //取消上一次的订阅
                mTimeCountDisposableQJZD.dispose();
            }
            cefanType = 0;
            qiaojiType = 10;
            fanshenType = 20;
        }

    }

    /**
     * 翻身选择器
     */
    private void initFanshenWindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.remind_turnovew_item_layout, null);
        RelativeLayout tv_irs_close = view.findViewById(R.id.tixing_item_fs_close);
        RelativeLayout tv_irs_1 = view.findViewById(R.id.tixing_item_fs_one);
        RelativeLayout tv_irs_2 = view.findViewById(R.id.tixing_item_fs_two);
        RelativeLayout tv_irs_3 = view.findViewById(R.id.tixing_item_fs_three);
        RelativeLayout tv_irs_4 = view.findViewById(R.id.tixing_item_fs_four);
        switch (fanshenType) {
            case 20:
                tv_irs_close.setVisibility(View.GONE);
                break;
            case 21:
                tv_irs_1.setVisibility(View.GONE);
                break;
            case 22:
                tv_irs_2.setVisibility(View.GONE);
                break;
            case 23:
                tv_irs_3.setVisibility(View.GONE);
                break;
            case 24:
                tv_irs_4.setVisibility(View.GONE);
                break;
        }
        tv_irs_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fanshenType = 20;
                FSSelect(fanshenType, tixing_item_fanshen_item_text, tixing_item_fanshen_item, fsPop);
            }
        });
        tv_irs_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fanshenType = 21;
                FSSelect(fanshenType, tixing_item_fanshen_item_text, tixing_item_fanshen_item, fsPop);
            }
        });
        tv_irs_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fanshenType = 22;
                FSSelect(fanshenType, tixing_item_fanshen_item_text, tixing_item_fanshen_item, fsPop);
            }
        });
        tv_irs_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fanshenType = 23;
                FSSelect(fanshenType, tixing_item_fanshen_item_text, tixing_item_fanshen_item, fsPop);
            }
        });
        tv_irs_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fanshenType = 24;
                FSSelect(fanshenType, tixing_item_fanshen_item_text, tixing_item_fanshen_item, fsPop);
            }
        });
        fsPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        fsPop.setFocusable(true);
        fsPop.setOutsideTouchable(true);
        fsPop.setBackgroundDrawable(new ColorDrawable());
        fsPop.showAsDropDown(tixing_item_fanshen_item);
    }

    /**
     * 敲击震动选择器
     */
    private void initQiaojiWindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.remind_hit_item_layout, null);
        RelativeLayout tv_irs_close = view.findViewById(R.id.tixing_item_qjzd_item_close);
        RelativeLayout tv_irs_1 = view.findViewById(R.id.tixing_item_qjzd_one);
        RelativeLayout tv_irs_2 = view.findViewById(R.id.tixing_item_qjzd_two);
        RelativeLayout tv_irs_3 = view.findViewById(R.id.tixing_item_qjzd_three);
        RelativeLayout tv_irs_4 = view.findViewById(R.id.tixing_item_qjzd_four);
        switch (qiaojiType) {
            case 10:
                tv_irs_close.setVisibility(View.GONE);
                break;
            case 11:
                tv_irs_1.setVisibility(View.GONE);
                break;
            case 12:
                tv_irs_2.setVisibility(View.GONE);
                break;
            case 13:
                tv_irs_3.setVisibility(View.GONE);
                break;
            case 14:
                tv_irs_4.setVisibility(View.GONE);
                break;
        }
        tv_irs_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiaojiType = 10;
                QJZDSelect(qiaojiType, tixingItemZhendongItemText, qjzdPop);
            }
        });
        tv_irs_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiaojiType = 11;
                QJZDSelect(qiaojiType, tixingItemZhendongItemText, qjzdPop);
            }
        });
        tv_irs_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiaojiType = 12;
                QJZDSelect(qiaojiType, tixingItemZhendongItemText, qjzdPop);
            }
        });
        tv_irs_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiaojiType = 13;
                QJZDSelect(qiaojiType, tixingItemZhendongItemText, qjzdPop);
            }
        });
        tv_irs_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiaojiType = 14;
                QJZDSelect(qiaojiType, tixingItemZhendongItemText, qjzdPop);
            }
        });
        qjzdPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        qjzdPop.setFocusable(true);
        qjzdPop.setOutsideTouchable(true);
        qjzdPop.setBackgroundDrawable(new ColorDrawable());
        qjzdPop.showAsDropDown(tixingItemZhendongItem);
    }

    /**
     * 侧翻选择器
     */
    private void initCefanWindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.remind_rollover_item_layout, null);
        RelativeLayout tv_irs_close = view.findViewById(R.id.tixing_item_cf_item_close);
        RelativeLayout tv_irs_1 = view.findViewById(R.id.tixing_item_cefan_one);
        RelativeLayout tv_irs_2 = view.findViewById(R.id.tixing_item_cefan_two);
        RelativeLayout tv_irs_3 = view.findViewById(R.id.tixing_item_cefan_three);
        RelativeLayout tv_irs_4 = view.findViewById(R.id.tixing_item_cefan_four);
        switch (cefanType) {
            case 0:
                tv_irs_close.setVisibility(View.GONE);
                break;
            case 1:
                tv_irs_1.setVisibility(View.GONE);
                break;
            case 2:
                tv_irs_2.setVisibility(View.GONE);
                break;
            case 3:
                tv_irs_3.setVisibility(View.GONE);
                break;
            case 4:
                tv_irs_4.setVisibility(View.GONE);
                break;
        }
        tv_irs_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cefanType = 0;
                cefanSelect(cefanType, tixingItemCloseText, cefanPop);
            }
        });
        tv_irs_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cefanType = 1;
                cefanSelect(cefanType, tixingItemCloseText, cefanPop);
            }
        });
        tv_irs_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cefanType = 2;
                cefanSelect(cefanType, tixingItemCloseText, cefanPop);
            }
        });
        tv_irs_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cefanType = 3;
                cefanSelect(cefanType, tixingItemCloseText, cefanPop);
            }
        });
        tv_irs_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cefanType = 4;
                cefanSelect(cefanType, tixingItemCloseText, cefanPop);
            }
        });
        cefanPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        cefanPop.setFocusable(true);
        cefanPop.setOutsideTouchable(true);
        cefanPop.setBackgroundDrawable(new ColorDrawable());
        cefanPop.showAsDropDown(tixingItemClose);

    }

    @OnClick({R.id.tixing_item_fanshen_item, R.id.tixing_item_close, R.id.tixing_item_zhendong_item})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tixing_item_close:
                initCefanWindow();
                break;
            case R.id.tixing_item_zhendong_item:
                initQiaojiWindow();
                break;
            case R.id.tixing_item_fanshen_item:
                initFanshenWindow();
                break;
        }
    }

    private void cefanSelect(int cefanType, TextView textView, PopupWindow popupWindow) {
        popupWindow.dismiss();
        switch (cefanType) {
            case 0:
                textView.setText(R.string.remind_off);
                break;
            case 1:
                textView.setText(R.string.remind_10m);
                break;
            case 2:
                textView.setText(R.string.remind_20m);
                break;
            case 3:
                textView.setText(R.string.remind_30m);
                break;
            case 4:
                textView.setText(R.string.remind_40m);
                break;
        }
        TimeCountCF(cefanType);
    }

    //敲击震动选择
    private void QJZDSelect(int cefanType, TextView textView, PopupWindow popupWindow) {
        popupWindow.dismiss();
        switch (cefanType) {

            case 10:
                textView.setText(R.string.remind_off);
                break;
            case 11:
                textView.setText(R.string.remind_1h);
                break;
            case 12:
                textView.setText(R.string.remind_2h);
                break;
            case 13:
                textView.setText(R.string.remind_4h);
                break;
            case 14:
                textView.setText(R.string.remind_6h);
                break;
        }
        TimeCountQJZD(cefanType);
    }

    private void FSSelect(int cefanType, TextView textView, RelativeLayout relativeLayout, PopupWindow popupWindow) {
        popupWindow.dismiss();
        switch (cefanType) {

            case 20:
                textView.setText(R.string.remind_off);
                break;
            case 21:
                textView.setText(R.string.remind_30m);
                break;
            case 22:
                textView.setText(R.string.remind_60m);
                break;
            case 23:
                textView.setText(R.string.remind_90m);
                break;
            case 24:
                textView.setText(R.string.remind_120m);
                break;
        }
        TimeCountFS(cefanType);
    }

    private int timeLongCF = 0;
    private int timeLongQJZD = 0;
    private int timeLongFS = 0;
    private Disposable mTimeCountDisposableCF;
    private Disposable mTimeCountDisposableQJZD;
    private Disposable mTimeCountDisposableFS;

    /**
     * 侧翻倒计时
     */
    public void TimeCountCF(int num) {
        switch (num) {
            case 0:
                timeLongCF = 0;
                break;
            case 1:
                timeLongCF = 600;
                break;
            case 2:
                timeLongCF = 1200;
                break;
            case 3:
                timeLongCF = 1800;
                break;
            case 4:
                timeLongCF = 2400;
                break;
        }
        Log.d("yangshuoCF", timeLongCF + "");
        if (timeLongCF != 0) {
            cfTime.setVisibility(View.VISIBLE);
            if (mTimeCountDisposableCF != null) {
                //取消上一次的订阅
                mTimeCountDisposableCF.dispose();
            }
//        //0-count依次输出，延时0s执行，每1s发射一次。
            mTimeCountDisposableCF = Flowable.intervalRange(0, timeLongCF, 0, 1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(aLong -> {
                        cfTime.setText(generateTime(timeLongCF -= 1));
                    })
                    .doOnComplete(() -> {
                        if (mTimeCountDisposableCF != null) {
                            mTimeCountDisposableCF.dispose();
                        }
//                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.REMIND_MESSAGE);
                        Intent intent = new Intent();
                        intent.setClass(MyApp.getAppContext(), RemindNextActivity.class);
                        intent.putExtra("Remind_type", 0);
                        startActivity(intent);
                        cfTime.setVisibility(View.GONE);
                        tixingItemCloseText.setText(R.string.remind_off);
                        cefanType = 0;
                    })
                    .subscribe();
        } else {
            if (mTimeCountDisposableCF != null) {
                mTimeCountDisposableCF.dispose();
            }
            cfTime.setVisibility(View.GONE);
        }
    }

    /**
     * 敲击震动倒计时
     */
    public void TimeCountQJZD(int qjzd) {
        switch (qjzd) {
            case 10:
                timeLongQJZD = 0;
                break;
            case 11:
                timeLongQJZD = 3600;
                break;
            case 12:
                timeLongQJZD = 7200;
                break;
            case 13:
                timeLongQJZD = 14400;
                break;
            case 14:
                timeLongQJZD = 21600;
                break;
        }
        Log.d("yangshuoQJZD", timeLongQJZD + "");
        if (timeLongQJZD != 0) {
            qjzdTime.setVisibility(View.VISIBLE);
            if (mTimeCountDisposableQJZD != null) {
                //取消上一次的订阅
                mTimeCountDisposableQJZD.dispose();
            }
//        //0-count依次输出，延时0s执行，每1s发射一次。
            mTimeCountDisposableQJZD = Flowable.intervalRange(0, timeLongQJZD, 0, 1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(aLong -> {
                        qjzdTime.setText(generateTime(timeLongQJZD -= 1));
                    })
                    .doOnComplete(() -> {
                        if (mTimeCountDisposableQJZD != null) {
                            mTimeCountDisposableQJZD.dispose();
                        }
//                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.REMIND_MESSAGE);
                        Intent intent = new Intent();
                        intent.setClass(MyApp.getAppContext(), RemindNextActivity.class);
                        intent.putExtra("Remind_type", 1);
                        startActivity(intent);
                        qjzdTime.setVisibility(View.GONE);
                        tixingItemZhendongItemText.setText(R.string.remind_off);
                        qiaojiType = 10;
                    })

                    .subscribe();
        } else {
            if (mTimeCountDisposableQJZD != null) {
                mTimeCountDisposableQJZD.dispose();
            }
            qjzdTime.setVisibility(View.GONE);
        }
    }

    /**
     * 翻身倒计时
     */
    public void TimeCountFS(int fs) {
        switch (fs) {
            case 20:
                timeLongFS = 0;
                break;
            case 21:
                timeLongFS = 1800;
                break;
            case 22:
                timeLongFS = 3600;
                break;
            case 23:
                timeLongFS = 5400;
                break;
            case 24:
                timeLongFS = 7200;
                break;
        }
        Log.d("yangshuoFS", timeLongFS + "");
        if (timeLongFS != 0) {
            fsTime.setVisibility(View.VISIBLE);
            if (mTimeCountDisposableFS != null) {
                //取消上一次的订阅
                mTimeCountDisposableFS.dispose();
            }
//        //0-count依次输出，延时0s执行，每1s发射一次。
            mTimeCountDisposableFS = Flowable.intervalRange(0, timeLongFS, 0, 1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(aLong -> {
                        fsTime.setText(generateTime(timeLongFS -= 1));
                    })
                    .doOnComplete(() -> {
                        if (mTimeCountDisposableFS != null) {
                            mTimeCountDisposableFS.dispose();
                        }
//                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.REMIND_MESSAGE);
                        Intent intent = new Intent();
                        intent.setClass(MyApp.getAppContext(), RemindNextActivity.class);
                        intent.putExtra("Remind_type", 2);
                        startActivity(intent);
                        fsTime.setVisibility(View.GONE);
                        tixing_item_fanshen_item_text.setText(R.string.remind_off);
                        fanshenType = 20;
                    })

                    .subscribe();
        } else {
            if (mTimeCountDisposableFS != null) {
                mTimeCountDisposableFS.dispose();
            }
            fsTime.setVisibility(View.GONE);
        }
    }
}
